#ifndef __COM_HPP__
#define __COM_HPP__

#include"Log.hpp"
#include<cerrno>
#include<cstring>
#include<iostream>
#include<sys/ipc.h>
#include<sys/types.h>
#include<string>
#include<sys/shm.h>
using namespace std;


const int size = 4096 ;   //4kb
const string pathname = "/home/_mian_yang";
const int proj_id = 0x88881;
Log log;


//获取key
key_t GetKey()
{
    key_t k = ftok(pathname.c_str(), proj_id);
    if (k == -1)
    {
        log(Fatal, "ftok error: %s", strerror(errno));//打印错误消息
        exit(1);
    }

    log(Info, "ftok sucess! key is: %d", k);
    return k;   
}



int GetShareMemHepler(int flag)
{
    key_t k = GetKey() ;
    int shmid = shmget(k, size, flag); //key, size, flag   //返回值是共享内存的标识符。 
    if (shmid == -1) 
    {
        log(Fatal, "creat share memory error: %s", strerror(errno));
        exit(2);
    }
    log(Info, "creat shar memory is success! shmget is: %d", shmid);   

    return shmid;
}

int CreateShm()
{
    return GetShareMemHepler(IPC_CREAT | IPC_EXCL | 0666);
}

int GetShm()
{
    return GetShareMemHepler(IPC_CREAT);
}

#endif